package com.flash.CashAdministration.controller;

import com.flash.BusinessParameters.entity.Cash;
import com.flash.CashAdministration.entity.FundsAllot;
import com.flash.CashAdministration.service.FundsAllotService;
import com.flash.GlobalManagement.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FundsAllotController {

    @Autowired
    FundsAllotService fundsAllotService;

    @Resource
    Dbutil dbutil;


    @RequestMapping("/insertFundsAllot")
    @ResponseBody
    public String insertFundsAllot(FundsAllot fundsAllot, HttpServletRequest request){
        System.out.println("添加用户");
        fundsAllot.setFundsAllotId(NumberUtil.getNumber(TableIdUtil.FAID, TableNameUtil.FA,"FA"));
        System.out.println(fundsAllot.getFundsAllotId());
        HttpSession session = request.getSession();
        fundsAllot.setFundId((String) session.getAttribute("fundId"));
        String cashId = fundsAllotService.selectCash(fundsAllot.cashId);
        fundsAllot.setCashId(cashId);

        System.out.println(fundsAllot.getFundId());
        System.out.println(fundsAllot);
        int i = fundsAllotService.insertFundsAllot(fundsAllot);

        System.out.println(i);
        if (i>0){
            return "ok";
        }else {
            return "no";
        }
    }

    @RequestMapping("/deleteFundsAllot")
    @ResponseBody
    public int deleteFundsAllot(String fundsAllotId){
        System.out.println("删除用户");
        int i = fundsAllotService.deleteFundsAllot(fundsAllotId);
        System.out.println(i);
        return i;
    }

    @RequestMapping("/updateFundsAllots")
    @ResponseBody
    public int updateFundsAllot(FundsAllot fundsAllot,HttpServletRequest request){
        System.out.println("修改用户");
        HttpSession session = request.getSession();
        fundsAllot.setFundId((String) session.getAttribute("fundId"));
        System.out.println(fundsAllot.getFundId());
        System.out.println(fundsAllot);
        System.out.println("修改");
        int i = fundsAllotService.updateFundsAllot(fundsAllot);
        System.out.println(i);
        return i;
    }

    @RequestMapping("/selectFundsAllot")
    @ResponseBody
    public Map selectFundsAllot(String businessTime, Integer allotDirection, Integer allotType , int page, int limit){
        System.out.println("查询");
        System.out.println(businessTime);
        System.out.println(allotDirection);
        System.out.println(allotType);
        HashMap map = new HashMap();
        List<FundsAllot> fundsAllot = fundsAllotService.selectFundsAllotMap(businessTime,allotDirection,allotType, page, limit);
        for (int i = 0; i < fundsAllot.size(); i++) {
            FundsAllot fundsAllot1 = fundsAllot.get(i);
            if (fundsAllot1.getAllotType() == 1){
                fundsAllot1.setAllotTypes("存款利息");
            } else if (fundsAllot1.getAllotType() == 2) {
                fundsAllot1.setAllotTypes("申购赎回清算款");
            } else if (fundsAllot1.getAllotType() == 3) {
                fundsAllot1.setAllotTypes("买卖交易清算款");
            } else if (fundsAllot1.getAllotType() == 4) {
                fundsAllot1.setAllotTypes("债券利息");
            } else if (fundsAllot1.getAllotType() == 5) {
                fundsAllot1.setAllotTypes("存款业务");
            } else {
                fundsAllot1.setAllotTypes("俩费");
            }

            if (fundsAllot1.getAllotDirection() == 1){
                fundsAllot1.setAllotDirections("流入");
            } else if (fundsAllot1.getAllotDirection() == -1) {
                fundsAllot1.setAllotDirections("流出");
            }

        }
        int count = fundsAllotService.selectCountFundsAllot();
        System.out.println(fundsAllot);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",fundsAllot);
        return map;
    }
}
